﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Media;

namespace SydEnergiPrototype.Logic
{
    public class BrushParser : IBrushParser
    {
        public string ParseToString(SolidColorBrush brush)
        {
            var colors = GetColorProperties();

            foreach (KeyValuePair<string, object> colorPair in colors)
            {
                var color = (Color)colorPair.Value;
                if (brush.Color == color)
                    return colorPair.Key;
            }

            throw new Exception("Unable to parse color");
        }

        public SolidColorBrush ParseToBrush(string brush)
        {
            var colors = GetColorProperties();

            foreach (KeyValuePair<string, object> colorPair in colors)
            {
                if (brush.ToLower() == colorPair.Key.ToLower())
                    return new SolidColorBrush((Color) colorPair.Value);
            }
            throw new Exception("Unable to parse string to brush");
        }

        public static Dictionary<string, object> GetColorProperties()
        {
            Type t = typeof (Colors);
            const BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

            var map = new Dictionary<string, object>();
            foreach (var prop in t.GetProperties(flags))
            {
                map[prop.Name] = prop.GetValue(null, null);
            }
            return map;
        }

    }
}
